﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 约苗秒杀.Models;
using 约苗秒杀.Service;

namespace 约苗秒杀
{
    public partial class 成员 : Form
    {
        Form1 form;
        HttpService httpService;
        public 成员(Form1 form)
        {
            InitializeComponent();
            this.form = form;
            httpService = new HttpService(form);
        }

        private void 成员_Load(object sender, EventArgs e)
        {
            var datas = httpService.getMembers();
            if (datas == null) { form.SendMeg($"获取成员信息失败"); return; }
            form.SendMeg($"获取成员信息成功，获取到{datas.Count}个成员");
            foreach (var item in datas)
            {
                int index = this.dataGridView1.Rows.Add();
                dataGridView1.Rows[index].Cells[0].Value = item.Id;
                dataGridView1.Rows[index].Cells[1].Value = item.Name;
                dataGridView1.Rows[index].Cells[2].Value = item.IdCardNo;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var index = dataGridView1.CurrentRow.Index;
                if (dataGridView1.Rows[index].Cells[0].Value == null)
                {
                    form.SendMeg("未选择有效数据");
                    this.Close();
                    return;
                }
                Config.idCard = dataGridView1.Rows[index].Cells[2].Value.ToString();
                Config.memberName = dataGridView1.Rows[index].Cells[1].Value.ToString();
                Config.memberId = int.Parse(dataGridView1.Rows[index].Cells[0].Value.ToString());
                form.SendMeg($"设置成员信息成功，当前设置抢购人为：{Config.memberName}");
                this.Close();
            }
            catch (Exception ex)
            {
                form.SendMeg("发生异常--" + ex.Message);
                this.Close();
            }

        }
    }
}
